<template>
  <div class="zn-wrapper">

    <!--查询-->
    <div class="zn-search-header">
      <div class="search">
        <div v-for="item in searchData" :key="item.key" class="searchItem">
          <label for="">{{$t(`${name}.search.${item.key}`)}}：</label>
          <el-input size="small" :placeholder="$t('common.search')" v-model="search[item.key]" v-if="item.type === 'input'"></el-input>
          <el-select size="small" :placeholder="$t('common.search')" v-model="search[item.key]" v-if="item.type === 'select'">
            <el-option
              v-for="i in item.options"
              :key="i.value"
              :value="i.value"
              :label="i.label">
            </el-option>
          </el-select>
          <el-date-picker
            v-if="item.type === 'dateTime'"
            v-model="search[item.key]"
            type="datetime"
            size="small"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </div>
        <el-button size="mini" class="searchBtn" type="primary" icon="el-icon-search" @click="getList()"></el-button>
      </div>
    </div>
    <!--添加-->
    <div class="zn-op-header">
      <el-button size="mini" type='primary' icon="el-icon-plus" @click="addClick">{{$t('common.add')}}</el-button>
    </div>
    <div class="zn-select-up">
      <el-tooltip class="item" effect="dark" :content="$t('common.delAll')" placement="top">
        <el-button icon="el-icon-delete" size="mini" @click="delAll"></el-button>
      </el-tooltip>
    </div>
    <!--列表-->
    <div class="zn-main">
      <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" v-if="tableHeaderData.hasSelection">
        </el-table-column>
        <el-table-column v-for="header in tableHeaderData.data" :key="header.prop" :label="$t(`${name}.tableHead.${header.prop}`)" :prop="header.prop">
          <template slot-scope="scoped">
            <div v-if="header.type === 'img'">
              <img :src="scoped.row[header.prop]">
            </div>
            <div v-else-if="header.type === 'link'">
              <a :href="scoped.row[header.prop]">{{scoped.row[header.prop]}}</a>
            </div>
            <div v-else>
              {{scoped.row[header.prop]}}
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.operate')" width="170">
          <template slot-scope="scoped">
            <div class="operate">
              <el-tooltip class="item" effect="dark" :content="$t('common.view')" placement="top">
                <el-button icon="el-icon-view" size="mini" @click="checkClick(scoped.row,scoped.$index)"></el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" :content="$t('common.edit')" placement="top">
                <el-button icon="el-icon-edit" size="mini" @click="editClick(scoped.row,scoped.$index)"></el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" :content="$t('common.del')" placement="top">
                <el-button icon="el-icon-delete" size="mini" @click="deleteClick(scoped.row.id,scoped.$index)"></el-button>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="zn-select-down">
        <el-tooltip class="item" effect="dark" :content="$t('common.delAll')" placement="top">
          <el-button icon="el-icon-delete" size="mini" @click="delAll"></el-button>
        </el-tooltip>
      </div>
      <div class="zn-table-foot clearfix">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 50, 100, 200]"
          layout="sizes, total, prev, pager, next"
          :total="page.total">
        </el-pagination>
      </div>
    </div>

    <!-- 弹出新增框 -->
    <el-dialog custom-class="addTem" :title="!editModel ? $t('common.add') : $t('common.edit')" :visible.sync="addModel" class="addTempModel">
      <el-form label-width="130px" :model="form" :rules="rules" ref="form">
        <el-row>
          <el-form-item :label="$t('freightTemplate.sort')">
            <el-col :span="18"><el-input v-model="form.order"></el-input></el-col>
          </el-form-item>
          </el-row>
        <el-row>
          <el-form-item :label="$t('freightTemplate.freightTempName')" prop="name">
            <el-col :span="18"><el-input v-model="form.name"></el-input></el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="$t('freightTemplate.isDefault')">
            <el-col :span="18">
              <el-radio-group v-model="form.isDefault">
                <el-radio :label="0">{{$t('freightTemplate.yes')}}</el-radio>
                <el-radio :label="1">{{$t('freightTemplate.no')}}</el-radio>
              </el-radio-group>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="$t('freightTemplate.availability')">
            <el-col :span="18">
              <el-radio-group v-model="form.enabled">
                <el-radio :label="0">{{$t('freightTemplate.yes')}}</el-radio>
                <el-radio :label="1">{{$t('freightTemplate.no')}}</el-radio>
              </el-radio-group>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="$t('freightTemplate.billingMode')">
            <el-col :span="18">
              <el-radio-group v-model="form.type">
                <el-radio :label="0">{{$t('freightTemplate.billing')}}</el-radio>
                <el-radio :label="1">{{$t('freightTemplate.chargingByWeight')}}</el-radio>
              </el-radio-group>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="$t('freightTemplate.defaultFreight')">
            <el-col :span="4">{{$t('freightTemplate.firstWeight')}}</el-col>
            <el-col :span="4">{{$t('freightTemplate.firstCharge')}}</el-col>
            <el-col :span="4">{{$t('freightTemplate.continuousWeight')}}</el-col>
            <el-col :span="4">{{$t('freightTemplate.renewals')}}</el-col>
            <el-col :span="4">{{$t('freightTemplate.fullPackage')}}</el-col>
            <el-col :span="4"><el-input v-model="form.first"></el-input></el-col>
            <el-col :span="4"><el-input v-model="form.firstPrice"></el-input></el-col>
            <el-col :span="4"><el-input v-model="form.second"></el-input></el-col>
            <el-col :span="4"><el-input v-model="form.secondPrice"></el-input></el-col>
            <el-col :span="4"><el-input v-model="form.freeShipping"></el-input></el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="$t('freightTemplate.type')">
            <el-col :span="18">
              <el-radio-group v-model="form.specialAreasType">
                <el-radio :label="0">{{$t('freightTemplate.nonDistributionArea')}}</el-radio>
                <el-radio :label="1">{{$t('freightTemplate.distributionArea')}}</el-radio>
              </el-radio-group>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item :label="form.specialAreasType ? $t('freightTemplate.distributionArea'):$t('freightTemplate.nonDistributionArea')">
            <div><el-col :span="24">{{form.specialAreas}}</el-col></div>
            <div>
              <el-button type="primary" @click="checkArea">{{$t('freightTemplate.selectionArea')}}</el-button>
            </div>
            <div class="areaCheckModel" v-show="isShowAreaModel">
              <div class="areaCheckBox">
                <div class="areaModelHeader">
                  <el-col :span="23" style="padding-left:15px;"><label>{{$t('freightTemplate.selectionArea')}}</label></el-col>
                  <i class="el-icon-close" @click="isShowAreaModel = false"></i>
                </div>
                <div class="elTree">
                  <div class="elTreeRow" v-for="(i,k) in areaData" :key="k">
                    <i :class="!i.isShow ? 'el-icon-caret-right':'el-icon-caret-bottom'" @click="i.isShow = !i.isShow;getCity(i.id,k)"></i>
                    <el-checkbox v-model="i.checked" @change="changeTree($event,k)">{{i.label}}</el-checkbox>
                    <div v-show="i.isShow">
                      <div class="treeSon" v-for="(item,key) in i.children" :key="key">
                        <el-checkbox v-model="item.checked" @change="changeSonTree($event,k,key)">{{item.label}}</el-checkbox>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="areaModelFooter">
                  <el-button @click="isShowAreaModel = false">{{$t('common.cancel')}}</el-button>
                  <el-button type="primary" @click="saveArea">{{$t('common.confirm')}}</el-button>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-row>
        <div class="footer">
          <el-button type="primary" @click="submitForm('form',editModel ? true : false)">{{$t('common.confirm')}}</el-button>
          <el-button @click="cancleForm">{{$t('common.cancel')}}</el-button>
        </div>
      </el-form>
    </el-dialog>

    <!-- 弹出查看框 -->
    <el-dialog custom-class="viewTem" :title="$t('common.view')" :visible.sync="checkModel">
      <el-form label-width="100px">
        <el-form-item v-for="(item,index) in tableHeaderData.data" :key="index" :label="$t(`${name}.tableHead.${item.prop}`)">
          <label for="">{{checkObj[item.prop]}}</label>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="checkModel = false">{{$t('common.cancel')}}</el-button>
        <el-button type="primary" @click="checkModel = false">{{$t('common.confirm')}}</el-button>
      </span>
    </el-dialog>

    <!-- 弹出编辑框
    <el-dialog custom-class="updateTem" :title="$t('common.edit')" :visible.sync="editModel">
      <el-form label-width="100px">
        <el-form-item v-for="(item,index) in tableHeaderData.data" :key="index" :label="$t(`${name}.tableHead.${item.prop}`)">
          <el-input v-model="editObj[item.prop]"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editModel = false">{{$t('common.cancel')}}</el-button>
        <el-button type="primary" @click="editData">{{$t('common.confirm')}}</el-button>
      </span>
    </el-dialog> -->

  </div>
</template>

<script>
import * as config from "./data";
import local from "./local";
import api from "@/api/freightTemplate.service.js";
import areaApi from "@/api/areas.service.js";
import { setTimeout } from "timers";

export default {
  data() {
    return {
      areaString: "",
      props: {
        isLeaf: "leaf"
      },
      form: {
        enabled: 0,
        first: 0,
        firstPrice: 0,
        freeShipping: 0,
        isDefault: 1,
        name: "",
        order: "",
        second: 0,
        secondPrice: 0,
        specialAreas: "",
        specialAreasType: 0,
        type: 0
      }, // 添加弹框表单
      rules: {
        name: [{ required: true, message: "此项必须填写", trigger: "blur" }]
      }, //添加弹框规则
      isShowAreaModel: false,
      areaData: [],
      name: null,
      search: {},
      searchData: [],
      tableData: [],
      tableHeaderData: {},
      addModel: false, //是否显示添加模态框
      checkModel: false, //是否显示查看模态框
      editModel: false, //是否显示编辑模态框
      delModel: false, //删除确认
      addObj: {}, //当前添加对象
      checkObj: {}, //当前查看对象
      editObj: {}, //当前编辑对象
      delObj: "", //当前删除的对象
      key: "", //搜索关键字
      currentPage: 1,
      page: {
        pageNo: 1,
        pageSize: 10,
        total: null
      },
      selectAll: null
    };
  },
  created() {
    this.$i18n.mergeLocaleMessage("en", local.en);
    this.$i18n.mergeLocaleMessage("zh", local.zh);
    this.$i18n.mergeLocaleMessage("ko", local.ko);
    Object.keys(config).map(res => {
      this[res] = config[res];
    });
    this.getList();
  },
  methods: {
    cancleForm () {
      this.addModel = false
      this.form = {
        enabled: 0,
        first: 0,
        firstPrice: 0,
        freeShipping: 0,
        isDefault: 1,
        name: "",
        order: "",
        second: 0,
        secondPrice: 0,
        specialAreas: "",
        specialAreasType: 0,
        type: 0
      }
    },
    saveArea() {
      this.form.specialAreas = this.areaString;
      this.isShowAreaModel = false;
    },
    setSpecialAreas(flag, label) {
      if (flag && !this.areaString.includes(label)) {
        this.areaString += label + ";";
      } else if (!flag && this.areaString.includes(label)) {
        let lab = label + ";";
        this.areaString = this.areaString.replace(lab, "");
      }
    },
    changeSonTree(flag, ind, index) {
      if (!flag) {
        this.areaData[ind].checked = false;
      } else {
        let f = this.areaData[ind].children.every(e => e.checked === true);
        f ? (this.areaData[ind].checked = true) : null;
      }
      this.setSpecialAreas(flag, this.areaData[ind].children[index].label);
    },
    changeTree(flag, i) {
      this.areaData[i].isShow = true;
      if (!this.areaData[i].children) {
        this.getCity(this.areaData[i].id, i, true);
      } else {
        this.areaData[i].children.forEach((e, ind) => {
          e.checked = flag;
          this.setSpecialAreas(flag, e.label);
        });
      }
    },
    getList() {
      // 调接口拿数据
      this.search.pageNo = this.page.pageNo;
      this.search.pageSize = this.page.pageSize;
      api.list(this.search).then(res => {
        this.tableData = res.values;
        this.page.total = res.total;
      });
    },
    handleSelectionChange(select) {
      this.selectAll = select;
    },
    //      批量删除
    delAll() {
      if (this.selectAll && this.selectAll.length > 0) {
        this.selectAll.map(res => {
          this.delObj += `${res.id},`;
        });
        this.delObj = this.delObj.slice(0, this.delObj.length - 1);
        this.deleteClick(this.delObj);
      } else {
        this.$message({
          message: this.$t("common.delAllTip"),
          type: "warning"
        });
      }
    },
    // 打开区域选择框
    checkArea() {
      this.areaString = this.form.specialAreas;
      if (this.areaData.length == 0 || this.editModel) {
        this.areaData = []
        areaApi
          .proList({ keywords: "", pageNo: 1, pageSize: 20, type: 2 })
          .then(res => {
            let data = res.values;
            data.forEach(e => {
              this.areaData.push({
                id: e.id,
                label: e.name,
                checked: false,
                isShow: false
              });
            });
          });
      }
      this.isShowAreaModel = true;
    },
    getCity(id, i, f) {
      let city = [];
      areaApi
        .cityList({
          keywords: "",
          pageNo: 1,
          pageSize: 50,
          parentId: id
        })
        .then(res => {
          let data = res.values;
          data.forEach(e => {
            city.push({
              id: e.id,
              label: e.name,
              checked: this.areaData[i].checked
            });
          });
          this.$set(this.areaData[i], "children", city);
          if (!!f) {
            this.areaData[i].children.forEach((e, ind) => {
              e.checked = true;
              this.setSpecialAreas(true, e.label);
            });
          }
        });
    },
    // 添加click
    addClick() {
      this.addModel = true;
      this.addObj = {};
    },
    // 查看click
    checkClick(data, index) {
      this.checkObj = data;
      this.checkModel = true;
    },
    // 编辑click
    editClick(data, index) {
      this.editObj = data;
      this.form = data
      this.editModel = true;
      this.addModel = true
    },
    // 删除click
    deleteClick(data, index) {
      this.$confirm(this.$t("common.deleteTip"), this.$t("common.tip"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning"
      }).then(() => {
        api.del(data).then(res => {
          this.getList();
        });
      });
    },
    handleCurrentChange(pageNo) {
      this.page.pageNo = pageNo;
      this.getList();
    },
    handleSizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList();
    },
    // 添加数据
    submitForm(formName,f) {
      debugger
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (f) {
            this.editData()
            return
          }
          api.add(this.form).then(res => {
            console.log('res', res)
            this.getList()
            this.cancleForm()
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 编辑数据
    editData() {
      this.editModel = false;
      api.update(this.form).then(res => {
        this.getList()
        this.cancleForm()
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.footer {
  text-align: center;
  margin-top: 20px;
}
.zn-op-header {
  margin-bottom: 16px;
}
.good-detail {
  position: relative;
  padding-left: 80px;
  min-height: 80px;
}
.good-detail .good-pic {
  position: absolute;
  left: 0;
  top: 0;
}
.good-detail .good-pic img {
  height: 80px;
  width: 80px;
  padding: 1px;
  border: 1px solid #efefef;
}
.good-detail .good-content {
  width: 260px;
  padding: 10px;
}
.input-with-select {
  width: 300px;
}
.addTem,
.el-form {
  display: grid;
  grid-template-columns: 50% 50%;
}

.updateTem .el-form {
  display: grid;
  grid-template-columns: 50% 50%;
}

.viewTem .el-form {
  display: grid;
  grid-template-columns: 50% 50%;
}
.zn-search-header {
  display: flex;
  justify-content: space-between;
  border: 1px solid #ddd;
  padding-top: 10px;
  margin-bottom: 10px;
}
.search {
  display: flex;
  flex-wrap: wrap;
}
.searchItem {
  margin-right: 20px;
  margin-bottom: 10px;
  width: 300px;
  display: flex;
  align-items: center;
  label {
    flex: 1;
    text-align: right;
  }
}
.searchItem {
  .el-select {
    width: 200px !important;
  }
  .el-input {
    width: 200px !important;
  }
}
.searchBtn {
  height: 32px;
}
.operate {
  display: flex;
}
.zn-select-up {
  margin-bottom: 16px;
}
.zn-select-down {
  margin-top: 16px;
}
</style>

<style>
.elTreeRow i {
  margin: 0 10px;
}
.treeSon {
  padding-left: 40px;
}
[class*=" el-icon-"],
[class^="el-icon-"] {
  cursor: pointer;
}
.zn-main .el-table__header-wrapper .cell {
  font-weight: normal;
  color: #000;
}
.addTempModel .el-form-item__content .el-col {
  border: 0;
  padding: 0;
}
.addTempModel .el-col-4 {
  margin-right: 10px;
}
.addTempModel .el-form {
  display: block;
}
.addTempModel .el-form-item {
  margin-bottom: 0;
}
.addTempModel .el-row {
  margin-top: 10px !important;
}
.addTempModel .el-dialog__body {
  padding-top: 0;
}
.addTempModel .el-dialog__header {
  padding-bottom: 0;
}
.areaCheckModel {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9999;
}
.areaCheckBox {
  width: 600px;
  margin: 50px auto;
}
.areaModelHeader,
.areaModelFooter {
  background-color: #f4f5f9;
}
.areaModelFooter {
  text-align: center;
  padding: 10px 0;
}
.areaCheckBox .elTree {
  height: 300px;
  overflow-y: auto;
  background: #fff;
}
</style>


